package com.superm.service.impl;

import com.superm.entity.Department;
import com.superm.entity.Emp;
import com.superm.entity.EmpJoPo;
import com.superm.mappers.EmpDutiesMapper;
import com.superm.mappers.EmpMapper;
import com.superm.service.EmpService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMp;
    @Autowired
    private EmpDutiesMapper empDutiesMp;
    /**
     * 实现用户认证登录
     * @param emp
     * @return
     */
    @Override
    public Emp empLogin(Emp emp){
        Emp emp1 =  empMp.login(emp);
        return emp1;
    }

    @Override
    public Integer EmpCount(Integer did) {
        return empMp.EmpCount(did);
    }


    /**
     * 实现根据用户信息查询用户
     * @param empJoPo
     * @return
     */
    @Override
    public List<Emp> selectLikeEmpList(EmpJoPo empJoPo,Integer pageNo,Integer pageSize) {
        Emp emp = new Emp();
        //将代替类的值获取过来
        BeanUtils.copyProperties(empJoPo,emp);
        //放入编号
        emp.seteDepartment(new Department(empJoPo.getEDepartment()));
        return empMp.allEmp(emp,(pageNo-1)*pageSize,pageSize);
    }

    /**
     * 实现查询总条数
     * @param empJoPo
     * @return
     */
    @Override
    public int selectEmpCount(EmpJoPo empJoPo) {
        Emp emp = new Emp();
        //将代替类的值获取过来
        BeanUtils.copyProperties(empJoPo,emp);
        //放入编号
        emp.seteDepartment(new Department(empJoPo.getEDepartment()));
        return empMp.selectCount(emp);
    }

    /**
     * 实现根据id查找用户信息
     * @param emp
     * @return
     */
    @Override
    public Emp selectIdUser(Emp emp) {
        return empMp.selectIdSeek(emp);
    }

    /**
     * 实现修改用户信息
     * @param empJoPo
     * @return
     */
    @Override
    public int updateUser(EmpJoPo empJoPo) {
        Emp emp1 = new Emp();
        //将代替类的值获取过来
        BeanUtils.copyProperties(empJoPo,emp1);
        //将密码转换为md5格式
        Md5Hash md5 = new Md5Hash(emp1.getePassword(),emp1.geteSalt(),1);
        emp1.setePassword(md5.toString());
        //放入编号
        emp1.seteDepartment(new Department(empJoPo.getEDepartment()));
        return empMp.updateInfoMation(emp1);
    }

    /**
     * 实现添加用户信息
     * @param empJoPo
     * @return
     */
    @Override
    public int addUser(EmpJoPo empJoPo){
        Emp emp1 = new Emp();
        //将代替类的值获取过来
        BeanUtils.copyProperties(empJoPo,emp1);
        //将密码转换为md5格式
        Md5Hash md5 = new Md5Hash(emp1.getePassword(),emp1.geteSalt(),1);
        emp1.setePassword(md5.toString());
        //放入编号
        emp1.seteDepartment(new Department(empJoPo.getEDepartment()));
        return empMp.addInfoMation(emp1);
    }

    /**
     * 实现删除用户信息
     * @param eId
     * @return
     */
    @Override
    public int deleteUser(Integer eId) {
        //根据当前用户的编号去角色关系表进行查询,如果查到了阻止删除
        if (empDutiesMp.selectInfoMationCount(eId)>0){
            return -1;
        }
        return  empMp.deleteInfoMation(eId);
    }

    /**
     * 实现修改用户状态
     * @param eId
     * @param eState
     * @return
     */
    @Override
    public int updateOneUser(Integer eId, String eState){
        return empMp.updateOneInfoMation(eId,eState);
    }

    @Override
    public List<Emp> allEmpRep() {
        return empMp.allEmpRep();
    }
}
